<template>
	<view>
		<view class="district" @touchmove.stop.prevent="moveStop">
			<!-- #ifdef H5  -->
			<view class="district_close" @click="close_district"><uni-icons type="closeempty" size="20"></uni-icons></view>
			<!-- #endif -->
			<view class="district_title">当前地区</view>
			<view class="district_title_row">
				<view class="district_title_row_name">{{ map.district || '地区' }}</view>
				<view class="district_title_row_btn" @click="reposition">自动获取</view>
			</view>
		</view>
		<view class="list">
			<view class="list_item"  @click="bindClick({areaName:'全城',district_code:''})">全城</view>
			<view class="list_item" v-for="(item, index) in district_list" :key="index" @click="bindClick(item)">{{ item.areaName }}</view>
		</view>
	</view>
</template>

<script>
import Map from '../../utils/map.js';
import Route from '../../utils/route.js';
export default {
	data() {
		return {
			map: {},
			// 地区列表
			district_list: []
		};
	},
	async onLoad() {
		this.map = uni.getStorageSync('map');
		await this.getCityList();
	},
	methods: {
		// 获取区县数据
		async getCityList(){
			uni.showLoading();
			this.district_list = await Route.district_list({cityId:this.map.city_code});
			this.district_list = this.district_list;
			uni.hideLoading();
		},
		// 关闭
		close_district() {
			uni.navigateBack();
		},
		// 重新定位
		async reposition() {
			uni.showToast({
				title: '正在定位',
				duration: 1500,
				icon: 'loading'
			});
			// #ifdef MP-WEIXIN
			this.map = await Map.wechat();
			// #endif
			// #ifdef H5
			this.map = await Map.H5();
			// #endif
			this.map = this.map;
			uni.setStorageSync('map', this.map);
			uni.showToast({
				title: '定位成功',
				duration: 1500,
				icon: 'none'
			});
		},
		// 手动选择地区
		bindClick(e) {
			this.map.district = e.areaName;
			this.map.district_code = e.areaId;
			uni.setStorageSync('map', this.map);
			uni.navigateBack()
		}
	}
};
</script>

<style lang="scss">
// 头部
.district {
	position: fixed;
	display: flex;
	flex-direction: column;
	justify-content: space-around;
	width: 690rpx;
	height: 150rpx;
	left: 0;
	right: 0;
	margin: 0 auto;
	&_close {
		position: absolute;
		right: 0;
		top: 10rpx;
		padding: 10rpx 20rpx;
	}
	&_title {
		font-size: $uni-font-size-lg;
		font-weight: 600;
		&_row {
			display: flex;
			align-items: center;
			&_name {
				font-size: $uni-font-size-lg;
				background: $template-background;
				color: $uni-text-color-inverse;
				padding: 6rpx 33rpx;
				text-align: center;
				border-radius: 40rpx;
			}
			&_btn {
				font-size: $uni-font-size-lg;
				background: $template-background;
				color: $uni-text-color-inverse;
				padding: 6rpx 33rpx;
				text-align: center;
				border-radius: 40rpx;
				margin-left: 33rpx;
			}
		}
	}
}

// 列表
.list {
	position: fixed;
	width: 100vw;
	margin-top: 150rpx;
	height: calc(100vh - 150rpx);
	overflow-y: auto;
	display: flex;
	flex-direction: column;
	&_item {
		width: 690rpx;
		height: 100rpx;
		line-height: 100rpx;
		margin: 0 auto;
		border-bottom: 1rpx solid #dedede;
	}
}
</style>
